<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_News extends Controller_Site {

    public function action_index() {
        $news = ORM::factory('news')->order_by('id', 'desc')->find_all();
        $this->content = View::factory('news/index')->set('news', $news);
    }

    public function action_new() {
        $form = View::factory('news/form')->set('action', URL::site('news/new'));

        $ckeditor = new CKEditor();
        $config = array(
                array('Source', '-',
                    'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-',
                    'Undo', 'Redo', '-',
                    'Find', 'Replace', '-',
                    'SelectAll', '-',
                    'Maximize', 'ShowBlocks'),
                array('Bold', 'Italic', 'Underline', 'Strike', '-',
                    'Subscript', 'Superscript', '-',
                    'NumberedList', 'BulletedList', '-',
                    'Outdent', 'Indent', 'Blockquote', '-',
                    'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-',
                    'Link', 'Unlink', 'Anchor', '-',
                    'Image', 'Flash', 'Table', 'HorizontalRule', 'SpecialChar'
                ),
                array('Format', 'Font', 'FontSize', '-', 'TextColor', 'BGColor')
            );

        $form->set('ckeditor', $ckeditor)->set('config', $config);

        if ($this->request->method() == 'POST') {
            try {
                $news = new Model_News();
                $form->set('news', $news);
                $this->processForm($news);

                $this->setFlash('notice', 'Sikeresen felvetted az új hírt!');
                $this->request->redirect('page/index');
            } catch (ORM_Validation_Exception $e) {
                $form->set('errors', $e->errors('models'));
            }
        }

        $this->content->set('form', $form);
    }

    protected function processForm($news) {
        $news->title = $this->request->post('title');
        $news->body = $this->request->post('body');
        $news->date = date('Y-m-d');
        $news->save();
    }

    public function action_show() {
        $news = new Model_News($this->request->param('id'));
        $this->content->set('news', $news);
    }

    public function action_edit() {
        $form = View::factory('news/form')->set('action', URL::site('news/edit/' . $this->request->param('id')));
        $news = new Model_News($this->request->param('id'));

        $ckeditor = new CKEditor();
        $config = array(
                array('Source', '-',
                    'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-',
                    'Undo', 'Redo', '-',
                    'Find', 'Replace', '-',
                    'SelectAll', '-',
                    'Maximize', 'ShowBlocks'),
                array('Bold', 'Italic', 'Underline', 'Strike', '-',
                    'Subscript', 'Superscript', '-',
                    'NumberedList', 'BulletedList', '-',
                    'Outdent', 'Indent', 'Blockquote', '-',
                    'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-',
                    'Link', 'Unlink', 'Anchor', '-',
                    'Image', 'Flash', 'Table', 'HorizontalRule', 'SpecialChar'
                ),
                array('Format', 'Font', 'FontSize', '-', 'TextColor', 'BGColor')
            );        

        $form->set('ckeditor', $ckeditor)->set('config', $config);

        if ($this->request->method() == 'POST') {
            try {
                $this->processForm($news, true);
                $this->setFlash('notice', 'Sikeresen módosítottad a hírt!');
                $this->request->redirect('news/index');
            } catch (ORM_Validation_Exception $e) {
                $form->set('errors', $e->errors('models'));
            }
        }
        $form->set('news', $news);
        $this->content->set('form', $form);
    }

    public function action_delete() {
        $news = new Model_News($this->request->param('id'));
        $news->delete();
        $this->request->redirect('admin/tasks');
    }
}
